import {
  getModuleField,
  workModulePageSelect,
  workModuleTypePageSelect,
  workModuleWayPageSelect,
} from '@/api/order'
import { ref } from 'vue'

/**
 * 获取工单模块下拉选项
 */
export const useGetModuleOptions = () => {
  // 获取选项加载
  const getOptionsLoading = ref(false)

  // 获取选项关键字
  const getOptionsKeyword = ref('')

  // 下拉选项
  const options = ref<Array<OrderModuleOption>>([])

  /**
   * 获取下拉选项请求参数
   */
  const getOptionsParam = ref({
    limit: 50,
    page: 1,
    name: getOptionsKeyword.value,
    id: '',
    departmentId: '',
  })

  /**
   * 获取下拉选项
   */
  const getOptions = async () => {
    getOptionsLoading.value = true
    const res = await workModulePageSelect(getOptionsParam.value)
    getOptionsLoading.value = false
    options.value = res.data.data.rows
  }

  return {
    options,
    getOptionsParam,
    getOptionsLoading,
    getOptionsKeyword,
    getOptions,
  }
}

/**
 * 获取工单模块分类下拉选项
 */
export const useGetOrderModuleTypeOptions = () => {
  // 获取下拉选项加载
  const loading = ref(false)

  // 获取下拉选项请求参数
  const getOptionsParam = ref({
    limit: 50,
    page: 1,
    name: '',
  })

  // 下拉选项
  const options = ref<Array<OrderModuleTypeOptions>>([])

  /**
   * 获取下拉选项
   * @param {String} orderId - 工单id
   */
  const getOptions = async (orderId: string) => {
    loading.value = true
    const res = await workModuleTypePageSelect(getOptionsParam.value, orderId)
    options.value = res.data.data.rows
    loading.value = false
  }

  return {
    options,
    loading,
    getOptionsParam,
    getOptions,
  }
}

/**
 * 获取工单模块处理方式
 */
export const useGetOrderModuleHandleMethod = () => {
  // 获取下拉加载
  const loading = ref(false)

  // 获取下拉选项请求参数
  const getOptionsParam = ref({
    limit: 50,
    page: 1,
    name: '',
  })

  // 下拉选项
  const options = ref<Array<OrderModuleHandleMethodOption>>([])

  /**
   * 获取下拉选项
   * @param {String} moduleId - 模块id
   */
  const getOptions = async (moduleId: string) => {
    loading.value = true
    const res = await workModuleWayPageSelect(getOptionsParam.value, moduleId)
    options.value = res.data.data.rows
    loading.value = false
  }

  return {
    options,
    loading,
    getOptionsParam,
    getOptions,
  }
}

/**
 * 根据模块id获取自定义字段列表
 */
export const useGetCustomFieldList = () => {
  // 获取自定义字段加载
  const getLoading = ref(false)

  // 自定义字段
  const customFieldList = ref<Array<OrderCustomFieldForCreate>>([])

  /**
   * 获取自定义字段
   * @param {String} moduleId - 模块id
   */
  const getCustomFields = async (moduleId: string) => {
    getLoading.value = true
    const res = await getModuleField(moduleId)
    customFieldList.value = res.data.data.row
    getLoading.value = false
  }

  return {
    getLoading,
    customFieldList,
    getCustomFields,
  }
}
